package it.free.maya;

import java.util.Date;

import org.apache.log4j.Logger;

/**
 * Keep data for a solr server configurtions: primary and backup
 * @author Stefano Falconetti
 */
public class SolrShard {
	
	/**
	 * Define the separator in the shards' list
	 */
	public static final String SOLR_SHARD_SEPARATOR = ",";
	/**
	 * Name of the param to set in the query to use shards
	 */
	public static final String SOLR_SHARD_PARAM_NAME = "shards";
	
	private static Logger logger = Logger.getLogger(SolrShard.class);
	
	private SolrCore myPrimaryHost = null;
	private SolrCore myBackupHost = null;
	private SolrCore myHost = null;
	private Date lastSwitch = null;

	/**
	 * Class builder
	 * @param primaryHost is the primary host
	 * @param backupHost is the backup one
	 */
	public SolrShard(SolrCore primaryHost, SolrCore backupHost) {
		this.myPrimaryHost = primaryHost;
		this.myBackupHost = backupHost;
		this.myHost = this.myPrimaryHost;
		this.lastSwitch = new Date();
	}

	/**
	 * Change the current solr host: primary to backup and backup to primary. 
	 * This happens when a check on the host fails the ping test
	 */
	public void switchHost(){
		
		if(this.myHost.getHostType() == SolrShardType.PRIMARY){
		   logger.info("> Switching shard: [" + this.myHost + "] from [" + this.myHost.getHostType() + "] to [" + this.myBackupHost.getHostType() + "]");
		   this.myHost = this.myBackupHost;
		   this.lastSwitch = new Date();
		}else{
			  logger.info("< Switching shard: [" + this.myHost + "] from [" + this.myHost.getHostType() + "] to [" + this.myPrimaryHost.getHostType() + "]");
			  this.myHost = this.myPrimaryHost;
			  this.lastSwitch = new Date();
		}
	}

	/**
	 * @return the currently used active host
	 */
	public SolrCore getActiveHost() {
		return this.myHost;
	}
	
	/**
	 * @return the primary host
	 */
	public SolrCore getPrimaryHost() {
		return this.myPrimaryHost;
	}
	
	/**
	 * Give a representation of this object
	 * @return info about this object as a print friendly string
	 */
	public String toInfoString(){
	
		StringBuilder objectRepresentation = new StringBuilder();
		
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("===============================");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("===============================");
		
		objectRepresentation.append(System.getProperty("line.separator"));
		
		objectRepresentation.append("ACTIVE HOST:");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append(this.myHost.toString());
		objectRepresentation.append(System.getProperty("line.separator"));
		
		objectRepresentation.append("PRIMARY HOST:");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append(this.myPrimaryHost.toString());
		objectRepresentation.append(System.getProperty("line.separator"));
		
		objectRepresentation.append("BACKUP HOST:");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append(this.myBackupHost.toString());
		objectRepresentation.append(System.getProperty("line.separator"));
		
		objectRepresentation.append("Last switch executed on: [" + this.lastSwitch.toString() + "]");
		
		objectRepresentation.append(System.getProperty("line.separator"));
		
		objectRepresentation.append("===============================");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("===============================");
		
		return objectRepresentation.toString();
		
	}

	/**
	 * @return the lastSwitch
	 */
	public Date getLatestSwitchDate() {
		return this.lastSwitch;
	}
}
